import EventManager from "../Managers/EventManager";
import { EventTypes } from "../Managers/EventTypes";
import { SDKTools } from "../Managers/TaskSDK/SDKTools";
import { WebDataProxy } from "../Managers/TaskSDK/数据代理类/WebDataProxy";
import { AudioSystem } from "../SystemAudio/AudioSystem";
import { StorageSystem } from "../SystemStorage/StorageSystem";
import SDK from "./SDK";

export class KSSDK extends SDK {

    onEvents() {
        super.onEvents();

        EventManager.on(EventTypes.GameEvents.GameRun, this.onGameRun, this);
        EventManager.on(EventTypes.GameEvents.GameOver, this.onGameOver, this);
    }

    protected setAdCfg(): void {
        let banner = WebDataProxy.inst.AdWebData.m_AdWebData?.banner_adunits || "2300018565_03";
        this.adConfig.adBannerIdList = banner;
        let ad = "2300018565_01"
        if(WebDataProxy.inst.AdWebData.m_AdWebData){
            ad = WebDataProxy.inst.AdWebData.m_AdWebData.reward_adunits[0].ad_unit;
        }
        this.adConfig.adVideoIdList = [ad]
        let Interstitia = WebDataProxy.inst.AdWebData.m_AdWebData?.inset_adunits || "2300018565_02";
        this.adConfig.adInterstitialId = Interstitia; // 指定插屏广告ID
        this.adConfig.shareInfoArr = [{ title: "一起来挑战吧!", img: '' }]; // 自定义分享
        this.adConfig.app_key = SDKTools.appid;
        uniSdk.AdPlat.instance.setBannerMaxShowTimes(60);
    }

    interstitialAd: any = null;
    /**
       * 插屏广告
       */
    protected showInterstitial(d?: { success?: Function, fail?: Function, close?: Function }, cb2?) {
        console.log("showInterstitial");
        //触摸屏蔽
        // EventManager.emit(EventTypes.GameEvents.SetTouchMaskEnable,)
        let param = {
            adUnitId:this.adConfig.adInterstitialId
        };
        this.interstitialAd = SDKTools.m_ADClass.createInterstitialAd(param);
        if (this.interstitialAd) {
            this.interstitialAd.onClose(res => {
                // 插屏广告关闭事件 
                d && d.close && d.close();
                this.interstitialAd.destroy();
            })
            this.interstitialAd.onError(res => {
                // 插屏广告Error事件  
            })
            let p = this.interstitialAd.show()
            p.then(function (result) {
                // 插屏广告展示成功  
                console.log(`show interstitial ad success, result is ${result}`)
                d && d.success && d.success();
            }).catch(function (error) {
                // 插屏广告展示失败  
                console.log(`show interstitial ad failed, error is ${error.msg} ${error.code}`)
                d && d.fail && d.fail();
            })
        } else {
            console.log("创建插屏广告组件失败");
        }

    }

    /**
     * 显示原生模板广告
     */
    protected showCustomAd(adIndex: number, d?: { success?: Function, fail?: Function, close?: Function }) {
        console.log("showCustomAd:", adIndex);
        // 第一个参数 0 表示：使用原生模板广告配置中的第一个广告ID
        uniSdk.showCustomAd(adIndex, (showed: boolean) => {

            if (showed) {
                console.log("原生模板广告显示成功");
                d && d.success && d.success();
            }
            else {
                console.log("原生模板广告显示失败 ");
                d && d.fail && d.fail();
                // 原生模板广告失败，尝试用插屏广告顶上
                // uniSdk.showInterstitial();
            }

        }, () => {
            d && d.close && d.close();
            console.log("原生模板广告被用户关闭 ");
        }, this);
    }

    /**
       * 拉起激励视频广告
       */
    protected showRewardedVideo(cb: Function | { success: Function, fail: Function, cancel: Function }, idx = 0) {
        let success = null;
        let fail = null;
        let cancel = null;
        if (typeof cb === 'object') {
            success = cb.success;
            fail = cb.fail;
            cancel = cb.cancel;
        } else {
            success = cb;
        }
        //暂停游戏
        EventManager.emit(EventTypes.GameEvents.GamePause);
        AudioSystem.pauseBGM();
        //
        console.warn('观看视频中');
        uniSdk.showRewardedVideo(idx, (status: number) => {
            if (status == 1) {
                console.log("激励视频已观看完毕 ");
                success && success();
                //恢复游戏
                EventManager.emit(EventTypes.GameEvents.GameResume);
                AudioSystem.resumeBGM();
            }
            else if (status == 0) {
                console.log("激励视频未观看完毕 ");
                cancel && cancel();
                EventManager.emit(EventTypes.GameEvents.GameResume);
                AudioSystem.resumeBGM();
            }
            else {
                console.log("激励视频加载失败 ");
                fail && fail();
                EventManager.emit(EventTypes.GameEvents.GameResume);
                AudioSystem.resumeBGM();
            }

        }, this);
    }


    //#region -------------事件---------------
    onGameRun() {
        let asset = StorageSystem.getData().levelAssets;
        this.reportAldStageStart(asset.curLv);
    }
    onGameOver(isWin) {
        let asset = StorageSystem.getData().levelAssets;

        if (isWin) {
            this.reportAldStageEnd(asset.curLv);
        } else {
            this.reportAldStageFail(asset.curLv);
        }
    }

    //#endregion
}